$(document).ready(function($){
	var _project = null;
	$.ajax({
        type : 'GET',
        url : '/ManEnv/project/menu/',
        async : false,  //同步请求  
        timeout:1000,  
        success:function(project){
        	if (!jQuery.isEmptyObject(project)){
            _project = project;
        	//alert('');
            d = new dTree('d');
            //level 0
           d.add(0, -1, project.name);
            //level 1
             d.add(1, 0, '架构定义'); 
            d.add(2, 0, '设计模式'); 
            //level 3 
            d.add(3, 1, '系统架构','/ManEnv/aadl/schedule?projectId='+project.id,true); 
            //d.add(4, 1, 'AADL文档',''); 
            //level 3
            d.add(5, 2, '实体数据区','/ManEnv/project/' + project.id + '/entityZone/edit', false);
            d.add(6, 2, '总线通信','/ManEnv/project/' + project.id + '/design_pattern_component/BUS/edit', false); 
            d.add(7, 2, 'IO通信','/ManEnv/project/' + project.id + '/design_pattern_component/IO/edit', false);  
            d.add(8, 2, '信息显示','/ManEnv/project/' + project.id + '/design_pattern_component/DISPLAY/edit', false); 
            d.add(9, 2, '控制响应','/ManEnv/project/' + project.id + '/design_pattern_component/CONTROL/edit', false);
            //d.add(10, 2, '复用','/ManEnv/designPatternCategory/' + project.id + '/select', false); 
            //d.add(11, 2, '系统任务','/ManEnv/designPatternCategory/' + project.id + '/select', false); 
            //d.add(11,0, '代码','/ManEnv/project/' + project.id + '/generateCode',true);
            d.add(11,0, '代码','/ManEnv/projectMethod/' + project.id + '/list',false);
            //d.add(13,0, '代码','/ManEnv/project/' + project.id + '/realMachineCode',true); 
            //d.add(12,2,'导入Rhapsody文件','/ManEnv/project/' + project.id + '/importRhapsody',false);
            d.closeAll();
            $("#project-menu-tree").html(d.toString());
        	}
        },  
        error: function() {  
            alert("ERROR!");
        }  
    });
//	  var head = document.getElementsByTagName('head')[0];
//	  var js = document.createElement('script');
//	  js.setAttribute('type', 'text/javascript');
//	  js.setAttribute('src', '<c:url value="/js/jquery.contextmenu.r2.js"/>');
//	  head.appendChild(js);
	$('#sd3').contextMenu('myMenu1', {
        bindings: {
          'lookup': function(t) {
        	  if(!jQuery.isEmptyObject(_project.model)){
        		  window.location.href="/ManEnv/"+_project.model.uri.replace(/:/g, '/');
        	  }
          },
          'edit': function(t) {
        	  if(!jQuery.isEmptyObject(_project.model)){
        		  window.location.href="/ManEnv/"+_project.model.uri.replace(/:/g, '/')+"/update?projectId="+_project.id;
        	  }else{
        		  window.location.href="/ManEnv/aadl/schedule?projectId="+_project.id;
        	  }
          },
          'update': function(t) {
        	  if(!jQuery.isEmptyObject(_project.model)){
        		  window.location.href="/ManEnv/"+_project.model.uri.replace(/:/g, '/')+"/update?projectId="+_project.id;
        	  }
          },
          'export': function(t) {
        	  if(!jQuery.isEmptyObject(_project.model)){
        		  window.location.href="/ManEnv/rhapsody/" + _project.id + "/actor";
        	  }else{
        		  alert("请先编辑架构模板");
        	  }
          },
          'delete': function(t) {
        	  if(!jQuery.isEmptyObject(_project.model)){
        		  //window.location.href="/ManEnv/aadl/delete?projectId="+_project.id;
        		  $.ajax({
        		        type : 'GET',
        		        url : "/ManEnv/aadl/delete?projectId="+_project.id,
        		        async : false,  //同步请求  
        		        timeout:1000,  
        		        success:function(data){
        		        	alert("成功删除系统架构");
        		        	_project.model = null;
        		        },  
        		        error: function() {  
        		            alert("删除失败");
        		        }  
        		    });
        	  }
          }
        }
      });
	
	$('#dd2').contextMenu('designpattern-menu', {
        bindings: {
          'use': function(t) {
        		  //window.location.href="/ManEnv/designPatternCategory/" + _project.id + "/select";
        		  $.ajax({
      		        type : 'GET',
      		        url : "/ManEnv/designPatternCategory/" + _project.id + "/select",
      		        async : false,  //同步请求  
      		        timeout:1000,  
      		        success:function(data){
      		        	$("#working-area").html(data);
      		        },  
      		        error: function() {  
    		            alert("内部错误");
    		        }  
      		    });
          },
          'import': function(t) {
        		  //window.location.href="/ManEnv/project/" + _project.id + "/importRhapsody";
        		  $.ajax({
      		        type : 'GET',
      		        url : "/ManEnv/project/" + _project.id + "/importRhapsody",
      		        async : false,  //同步请求  
      		        timeout:1000,  
      		        success:function(data){
      		        	$("#working-area").html(data);
      		        },  
      		        error: function() {  
      		            alert("内部错误");
      		        }  
      		    });
          }
        }
      });
	

	
});

function drawTree(result){
	var d = new dTree('d');
	d.add(0, -1, '  项目列表');
	var index = 1;
	var projectIndex = 0;
	var projectIdArray = new Array();
	for (i in result){
		 ++projectIndex;
		 d.add(i+1,0 ,result[i].name,'/ManEnv/project/list/' + result[i].id);
		 projectIdArray[projectIndex -1] = result[i].id;
		 ++index;
	}
	for (var i = 1; i <= projectIndex; ++i){
		var tmp = index;
		d.add(index, i, '架构模板','#', '','','/ManEnv/img/dtree/calendar.svg'); ++index;
        d.add(index, tmp, '查看系统架构',''); ++index;
        d.add(index, tmp, '更新系统架构','');  ++index;
        d.add(index, tmp, '查看AADL文档',''); ++index;
        d.add(index, tmp, '下载AADL文档',''); ++index;
		tmp = index;
		d.add(index, i, '设计模式','#', '','','/ManEnv/img/dtree/notebook.svg'); ++index;
		d.add(index, tmp, '实体数据区','/ManEnv/entityZone/'+ projectIdArray[i-1] + '/list/'); ++index;
	    d.add(index, tmp, '总线通信','/ManEnv/project/' + projectIdArray[i-1] + '/design_pattern_component/BUS'); ++index;
        d.add(index, tmp, 'IO通信','/ManEnv/project/' + projectIdArray[i-1] + '/design_pattern_component/IO');  ++index;
        d.add(index, tmp, '信息显示','/ManEnv/project/' + projectIdArray[i-1] + '/design_pattern_component/DISPLAY'); ++index;
        d.add(index, tmp, '控制响应','/ManEnv/project/' + projectIdArray[i-1] + '/design_pattern_component/CONTROL'); ++index;
	}
	//document.write(d);
	//alert(d);
	jQuery(d,{}).appendTo("#project_menu");
	//$("#project_menu").append(d);
}







